

clear all
use "[Folder]\Panel_Firm_Lab_OccSplit_temp.dta"

keep if u==0



//Adjust by which lab quantity measure you use
	global M1Lab = 130
	global M2Lab = 130
	global FLab = 2
	distinct Ind if u==0
	global Inds = r(ndistinct) - 1
	global Sizes = 4
	global Cities = 42
	distinct year if u==0
	global Years = r(ndistinct) - 1
	global Res = 8
	global NbrOfParam =  $M1Lab +$M2Lab + $FLab + $Cities + $Sizes + $Years + $Inds + $Res
	disp $NbrOfParam

	
	
//Set native-born to reference group	
	rename lab_M1_107 RefGr
	rename lab_M1_1 lab_M1_107 
	rename lab_M2_107 lab_M2Nat
	rename lab_M2_1 lab_M2_107
	
	 


//Function evaluation program
     program nlceslab
                version 14.2
                syntax varlist [if], at(name)
                local log_VA: word 1 of `varlist'
	
	// Retrieve parameters out of at matrix				
				tempvar LabAgg temp_VA
				gen double `LabAgg' = 0 `if'
                gen double `temp_VA' = 0 `if'
				replace `LabAgg' = `LabAgg' + RefGr  `if'

				
				forvalues i=2(1)$M1Lab {
				tempname delta1 delta2 
				scalar `delta1' = `at'[1,`i']
				replace `LabAgg' = `LabAgg' + `delta1' * (lab_M1_`i')  `if'
				scalar `delta2' = `at'[1,129+`i']
				replace `LabAgg' = `LabAgg' + `delta2' * (lab_M2_`i')  `if'
				}
				
				tempname delta 
				scalar `delta' = `at'[1,$M1Lab +$M2Lab ]
				replace `LabAgg' = `LabAgg' + `delta' * (lab_M2Nat)  `if'
			
				
				tempname delta 
				scalar `delta' = `at'[1,$M1Lab +$M2Lab + 1  ]
				replace `LabAgg' = `LabAgg' + `delta' * (lab_FNat)  `if'
	
				tempname delta 
				scalar `delta' = `at'[1,$M1Lab +$M2Lab +2]
				replace `LabAgg' = `LabAgg' + `delta' * (lab_FFor)  `if'
	
	
				//Adj index for number of industry dummies
				forvalues i=1(1)$Inds {
				tempname Industry 
				scalar `Industry' = `at'[1,$M1Lab +$M2Lab + $FLab + `i']
				replace `temp_VA' = `temp_VA' + `Industry'*Ind`i' 	`if'			
				}
				//Adj index for number of firm size dummies
				forvalues i=1(1)$Sizes {
				tempname Size 
				scalar `Size' = `at'[1, $M1Lab +$M2Lab + $FLab  + $Inds +`i']
				replace `temp_VA' = `temp_VA' + `Size'*FirmSize_`i' `if'				
				}
				//Adj index for number of City dummies
				forvalues i=1(1)$Cities {
				tempname City 
				scalar `City' = `at'[1, $M1Lab +$M2Lab + $FLab  + $Inds + $Sizes +`i']
				replace `temp_VA' = `temp_VA' + `City'*k_`i' 	`if'			
				}
				//Adj index for number of year dummies
				forvalues i=1(1)$Years {
				tempname Year 
				scalar `Year' = `at'[1, $M1Lab +$M2Lab + $FLab  + $Inds + $Sizes +$Cities +`i']
				replace `temp_VA' = `temp_VA' + `Year'*y_`i' `if'
				}				


                
				// Now fill in dependent variable. Cut some higher-order interaction terms to reduce number of parameters (a bit of convergence issues). Trying the same for baseline indicates higher-order interactions make no difference at all. 

                tempname ic thetaL Cap Cap_Sq Cap_Qu TI TI_Sq TI_Qu TICap 
				scalar `ic' = `at'[1,1]
				scalar `thetaL' = `at'[1,$M1Lab +$M2Lab + $FLab + $Inds + $Sizes +$Cities + $Years + 1]
				scalar `Cap' = `at'[1,$M1Lab +$M2Lab + $FLab +  $Inds + $Sizes +$Cities + $Years + 2]
				scalar `Cap_Sq' = `at'[1,$M1Lab +$M2Lab + $FLab +  $Inds + $Sizes +$Cities + $Years + 3]
				scalar `Cap_Qu' = `at'[1,$M1Lab +$M2Lab + $FLab +  $Inds + $Sizes +$Cities + $Years + 4]
				
				scalar `TI' = `at'[1,$M1Lab +$M2Lab + $FLab +  $Inds + $Sizes +$Cities + $Years + 5]
				scalar `TI_Sq' = `at'[1,$M1Lab +$M2Lab + $FLab +  $Inds + $Sizes +$Cities + $Years + 6]
				scalar `TI_Qu' = `at'[1,$M1Lab +$M2Lab + $FLab +  $Inds + $Sizes +$Cities + $Years + 7]
			
				scalar `TICap' = `at'[1,$M1Lab +$M2Lab + $FLab +  $Inds + $Sizes +$Cities + $Years + 8]

				
				replace `temp_VA' = `temp_VA' + `ic' + `thetaL' *ln(`LabAgg') +  `Cap'*logCap + `Cap_Sq'*logCap_Sq + `Cap_Qu'*logCap_Qu + `TI'*logGrosInvTot + `TI_Sq'*logGrosInvTot_Sq + `TI_Qu'*logGrosInvTot_Qu  + `TICap'*logGrosInvTot*logCap `if'
				replace `log_VA' = `temp_VA'   `if'
            end
			
			
		
			
mat M = J(1,$NbrOfParam,1)

cd "[Output FOlder]\"
//Est. for upper half of non-routinization industries
nl ceslab @ logFtg_Foradlingsvarde if u==0 , nparameters($NbrOfParam) initial(M)
parmest, format(estimate min95 max95 %8.2f p %8.1e) saving(HC_OccNR, replace) omit empty 


///////////////////////////////////////////////////////////////
///First, low NR//////
///////////////////////////////////////////////////////////////
use "[Output FOlder]\HC_OccNR.dta", clear

drop parm
gen par_nr= regexr(eq,"b","")
destring par_nr, replace
replace par_nr = 0 if par_nr ==1
replace par_nr = 1 if par_nr ==107
replace par_nr = 300 if par_nr ==130
replace par_nr = 301 if par_nr ==131
keep if par_nr<130 

rename par_nr FodelseLandnamn

rename estimate HC_OccNR_nr12
drop if stderr==0
keep  HC_OccNR_nr12  FodelseLandnamn

merge 1:1 FodelseLandnamn using "[Folder]\MacroData_ToMona_v1"
drop if _merge==2
drop _merge

merge 1:1 FodelseLandnamn using "[Output Folder]\NROcc_Weights"
drop if _merge==2
drop _merge

gen Weights=Occ_Weight_NR12

reg HC_OccNR_nr12 Y003  [aw=Weights] if WVInd==1 & cognitive!=. & TR!=. 

reg HC_OccNR_nr12 Y003 loggdp05 [aw=Weights] if WVInd==1 & cognitive!=. & TR!=. 

///////////////////////////////////////////////////////////////
///Then, high NR//////
///////////////////////////////////////////////////////////////
use "[Output Folder]\HC_OccNR.dta", clear
drop parm
gen par_nr= regexr(eq,"b","")
destring par_nr, replace

keep if par_nr>130
replace par_nr=par_nr-129
keep if par_nr<130
replace par_nr = 1 if par_nr ==107

rename par_nr FodelseLandnamn

rename estimate HC_OccNR_nr34
drop if stderr==0
keep  HC_OccNR_nr34  FodelseLandnamn


merge 1:1 FodelseLandnamn using "[Folder]\MacroData_ToMona_v1"
drop if _merge==2
drop _merge

merge 1:1 FodelseLandnamn using "[Output Folder]\NROcc_Weights"
drop if _merge==2
drop _merge

gen Weights=Occ_Weight_NR34

reg HC Y003  [aw=Weights] if WVInd==1 & cognitive!=. & TR!=. 

reg HC Y003 loggdp05 [aw=Weights] if WVInd==1 & cognitive!=. & TR!=. 

///////////////////////////////////////////////////////////////
